package test;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.PublicKey;

public class exam05 {
    public static void main(String[] args) {
        new GuI().launch();
    }
}

class GuI extends JFrame {
    JTextField text1;
    JTextField text2;
    JButton bn;
    JTextField text3;
    int a, b, c;


    public void launch() {
        setTitle("计算器");
        setSize(400, 130);
        setLocation(400, 400);
        setResizable(false);

        Container co = this.getContentPane();

        text1 = new JTextField();
        text1.setBounds(30, 30, 50, 30);
        text1.addActionListener(new TextAction_1());

//        bn = new JComboBox();
//        bn.setBounds(90, 30, 50, 30);

        text2 = new JTextField();
        text2.setBounds(100, 30, 50, 30);
        text2.addActionListener(new TextAction_2());

        text3 = new JTextField();
        text3.setBounds(250, 30, 50, 30);

        bn = new JButton("=");
        bn.setBounds(170, 30, 50, 30);
        bn.addActionListener(new ButtomAction());

        co.add(text1);
        co.add(text2);
        co.add(text3);
        co.add(bn);

        setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    class TextAction_1 implements ActionListener, DocumentListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField tx = (JTextField) e.getSource();
            a = Integer.valueOf(tx.getText());
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            Document Do = e.getDocument();
            System.out.println(a);
            try {
                a = Integer.valueOf(Do.getText(0, Do.getLength()));
//                System.out.println(a);
            } catch (BadLocationException badLocationException) {
//                badLocationException.printStackTrace();
            }
        }

        @Override
        public void removeUpdate(DocumentEvent e) {

        }

        @Override
        public void changedUpdate(DocumentEvent e) {

        }
    }

    class TextAction_2 implements ActionListener, DocumentListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField tx = (JTextField) e.getSource();
            b = Integer.valueOf(tx.getText());
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            Document Do = e.getDocument();
            try {
                b = Integer.valueOf(Do.getText(0, Do.getLength()));
            } catch (BadLocationException badLocationException) {
//                badLocationException.printStackTrace();
            }
        }

        @Override
        public void removeUpdate(DocumentEvent e) {

        }

        @Override
        public void changedUpdate(DocumentEvent e) {

        }
    }

    class ButtomAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            c = a + b;

            text3.setText(c + "");
        }
    }
}